﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Threading;
using System.Windows.Threading;

namespace StaticView
{
    public partial class Viewer : PhoneApplicationPage
    {
        StaticField field;
        Display display;
        Thread displayThread;

        public Viewer()
        {
            InitializeComponent();

            // Create a field (ussually done in main page)
            field = new StaticField(new Vector3(0, 0, 1), new Vector3(1, 0, 0), new Vector3(0, 0, 0), new Photo());
            // Create a Display object (contain the Display thread)
            display = new Display(field, this);
            displayThread = new Thread(new ThreadStart(display.Run));

            displayThread.Start();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            // Disable the Display thread
            if (displayThread.ThreadState == ThreadState.Running)
                displayThread.Abort();
        }

        // Cross-thread caller
        #region Cross-thread caller
        public void setCanvas(ImageSource bmp)
        {
            Dispatcher.BeginInvoke(() =>
            {
                image.Source = bmp;
            });
        }
        public void Rotate(float angle)
        {
            Dispatcher.BeginInvoke(() =>
            {
                CompositeTransform ct = new CompositeTransform();
                ct.Rotation = angle;
                image.RenderTransform = ct;
            });
        }
        public void Visible(bool show)
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (show)
                    image.Visibility = System.Windows.Visibility.Visible;
                else
                    image.Visibility = System.Windows.Visibility.Collapsed;
            });
        }
        #endregion
    }
}